package in.muton.edushare;

import in.muton.edushare.auth.SessionClashResolverPage;
import in.muton.edushare.url.URLLoader;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.wicket.RequestCycle;
import org.apache.wicket.RestartResponseException;
import org.apache.wicket.markup.html.WebPage;

import com.edushare.common.constants.ApplicationConstants;
import com.edushare.common.constants.ConfigPropertyName;
import com.edushare.common.constants.URLConstants;
import com.edushare.common.global.SystemGlobals;
import com.edushare.common.orm.ORMInterface;

/**
 * All Edushare Wicket pages will extend this class not the standard WebPage.
 * Any global page level method/variable goes here. 
 * @author Naishe
 * @version 0.0.1-SNAPSHOT
 */
public class EduSharePage extends WebPage {

	private static final long serialVersionUID = -2750431315840201569L;
	private Log log = LogFactory.getLog(EduSharePage.class);
	
	public EduSharePage(){
		if(getEduShareSession().getConflictingUser()!=null){
			log.debug("User Login Conflict. Sending to Conflict resolve page.");
			RequestCycle.get().setRedirect(true);
			RequestCycle.get().setResponsePage(SessionClashResolverPage.class);
			return;
		}
		
		if(SystemGlobals.getInstance().getConfigProperty(ConfigPropertyName.SUPPORT_BOOT).equalsIgnoreCase("true")){
			throw new RestartResponseException(URLLoader.getPageClass(URLConstants.APP_CONFIG_PATH));
		}
	}
	
	public EduShareSession getEduShareSession(){
		return (EduShareSession)getSession();
	}
	
	/**
	 * Handy method to make ORMInterface available to all the pages, to access things like DataContext of Cayenne
	 * @return ORMInterface
	 */
	public ORMInterface getSessionORMInterface(){
		return getEduShareSession().getSessionORMInterface();
	}
	
	
	/** Send user to "Page Not Found (404)" page */
	public void pageNotFound(){
		throw new RestartResponseException(URLLoader.getPageClass(URLConstants.PAGE_NOT_FOUND_PATH));
	}
	
	/** Send User to unauthorized page */
	public void unauthorized(){
		throw new RestartResponseException(URLLoader.getPageClass(URLConstants.UNAUTHORIZED_PATH));
	}
}
